<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>
<%@include file="header.jsp"%>
<script language="JavaScript" type="text/javascript" src="../datePicker/WdatePicker.js"></script>
<script>
	function getRecommend(){
		listEditTable();
		listRecommendByRecommendTypeId();
	}
	function deleteRecommend(){
       	var ids= "";
       	var seqs = "";		
        $($("input[name='listRecommendTableRecordIdRecordId']:checked")).each(function(){
            	var idValue = this.id;
            	var key=idValue.substring("listRecommendTableRecordIdRecordId".length,idValue.length);
        		var  positionSeqKey="#listRecommendTableRecordIdPositionSeq"+key;
        		ids = ids + "," + this.value;
        		seqs = seqs + "," + $(positionSeqKey).val();
            }
       	);
 
		$.ajax({
		     	type: "POST",
		      	url: "deleteRecommend.action",
		      	data: {
		     			recommendTypeId: $.trim( $("#recommendTypeId")[0].value ),
		     			recommendIdsStr: ids,
		     			positionSeqsStr:seqs	         			
			},
		      	success: function(msg){
					listRecommendByRecommendTypeId();
			}
			}
		);
	}
	function updateRecommend(){
       	var ids= "";
       	var seqs = "";		
        $($("input[name='listRecommendTableRecordIdRecordId']:checked")).each(function(){
            	var idValue = this.id;
            	var key=idValue.substring("listRecommendTableRecordIdRecordId".length,idValue.length);
        		var  positionSeqKey="#listRecommendTableRecordIdPositionSeq"+key;
        		ids = ids + "," + this.value;
        		seqs = seqs + "," + $(positionSeqKey).val();
            }
       	);
		 $.ajax({
		      	type: "POST",
		       	url: "updateRecommend.action",
		       	data: {
	       			recommendTypeId: $.trim( $("#recommendTypeId")[0].value ),
	       			recommendIdsStr: ids,
	       			positionSeqsStr:seqs
		       	},
		       	success: function(msg){
		       		listRecommendByRecommendTypeId();
				}
		 }
		);		
	}
	function listRecommendByRecommendTypeId(){
		  $.ajax({
	        	type: "POST",
	         	url: "listRecommendByRecommendTypeId.action",
	         	data: {
	         			recommendTypeId: $.trim( $("#recommendTypeId")[0].value ),
	         			beginDate:$("#beginDate").value,
	         			endDate:$("#endDate").value
		 			 },
	         	success: function(msg){
		        	var data = eval(msg);
		        	var tableHtml ="<form><table width=\"98%\" border=\"0\" cellpadding=\"3\" cellspacing=\"2\" bgcolor=\"#FFFFFF\" align=\"center\">";
		        	var fieldsLabel = data.fieldsLabel+",顺序";
		        	var fls = fieldsLabel.split(",");
		        	tableHtml = tableHtml + "<tr>" ;
		        	var w = 100/(fls.length+1);
		        	for(var i=0;i<fls.length;i++){
		        		tableHtml=tableHtml +"<td width=\"%"+w+"\" nowrap background=\"images/bg_table.jpg\" bgcolor=\"#ECEAE6\">"+fls[i]+"</td>";
		        	}
		        	 tableHtml=tableHtml +"<td width=\"%"+w+"\" nowrap background=\"images/bg_table.jpg\" bgcolor=\"#ECEAE6\">操作</td>";
		        	 tableHtml = tableHtml + "</tr>" ;
		        	 var fields = data.fields;
		        	 var fs = fields.split(",");
					var recommends = data.oldRecords;
					
					if ( recommends!=null && recommends.length>0){
						for(var i=0;i<recommends.length;i++){
								tableHtml = tableHtml+"<tr bgcolor='#F1F3F5' onmouseover = \"this.style.backgroundColor = '#FFDCB0'\" onmouseout = \"this.style.backgroundColor = ''\" style=''>";
					        	for(var j=0;j<fs.length;j++){
						        	 var strExpress = "recommends[i]."+fs[j];
					        		 tableHtml = tableHtml+"<td>"+ eval(strExpress) +"</td>";
					        	}
					        	 tableHtml = tableHtml+"<td>"+recommends[i].positionSeq +"</td>";
						 		tableHtml = tableHtml+"<td><input id=\"listRecommendTableRecordIdRecordId"+i+"\" name=\"listRecommendTableRecordIdRecordId\" type=\"checkbox\" value=\""+ recommends[i].recommendId+"\"/>&nbsp;<input type='text' size='5' name='listRecommendTableRecordIdPositionSeq' id='listRecommendTableRecordIdPositionSeq"+i+"'  value='"+ recommends[i].positionSeq+"' /><td></tr>";
						}
					}
					var lastRow="<tr><td  colspan=\""+(fls.length+1)+"\"    nowrap background=\"images/bg_table.jpg\" bgcolor=\"#ECEAE6\"  align=\"center\"><input type='hidden' name='listRecommendTableRecordIdRecommendTypeId' id='listRecommendTableRecordIdRecommendTypeId' value='"+data.recommendTypeId+"'/><input type=\"button\" value=\"更新\" onclick=\"updateRecommend()\"/>&nbsp;<input type=\"button\" value=\"删除\" onclick=\"deleteRecommend()\"/><td></tr>";
					tableHtml = tableHtml+lastRow+"</table></form>";
					$("#listRecommendTable")[0].innerHTML=tableHtml;		        	
	         	}   
	      });
	}
	function saveRecommend(){
       	var ids= "";
       	var seqs = "";
        $($("input[name='recordId']:checked")).each(function(){
            	var idValue = this.id;
            	var key=idValue.substring("recordId".length,idValue.length);
        		var  positionSeqKey="#positionSeq"+key;
        		seqs=seqs+","+$(positionSeqKey).val();
        		ids=ids+","+this.value;      
            }
       	);
 
		$.ajax({
        	type: "POST",
         	url: "saveRecommend.action",
         	data: {
       			recommendTypeId: $.trim( $("#recommendTypeId")[0].value ),
       			recordIdsStr: ids,
       			positionSeqsStr:seqs
	 		},
         	success: function(msg){
	 			listRecommendByRecommendTypeId();
         	}   
      });
         
	}
	function listEditTable(){
		 $.ajax({
	        	type: "POST",
	         	url: "listEditTable.action",
	         	data: jQuery.param({
	         			"recommendTypeId": $.trim( $("#recommendTypeId")[0].value ),
	         			"beginDate":$("#beginDate").val(),
	         			"endDate":$("#endDate").val()
		 			 }),
	         	success: function(msg){
		        	var data = eval(msg);
		        	var tableHtml ="<form><table width=\"98%\" border=\"0\" cellpadding=\"3\" cellspacing=\"2\" bgcolor=\"#FFFFFF\" align=\"center\">";
		        	var fieldsLabel = data.fieldsLabel;
		        	var fls = fieldsLabel.split(",");
		        	tableHtml = tableHtml + "<tr>" ;
		        	var w = 100/(fls.length+1);
		        	for(var i=0;i<fls.length;i++){
		        		tableHtml=tableHtml +"<td width=\"%"+w+"\" nowrap background=\"images/bg_table.jpg\" bgcolor=\"#ECEAE6\">"+fls[i]+"</td>";
		        	}
		        	 tableHtml=tableHtml +"<td width=\"%"+w+"\" nowrap background=\"images/bg_table.jpg\" bgcolor=\"#ECEAE6\">操作</td>";
		        	 tableHtml = tableHtml + "</tr>" ;
		        	 var fields = data.fields;
		        	 var fs = fields.split(",");
		        	 var records = data.records;
		        	 if ( records!=null && records.length>0){
			        	for(var i=0;i<records.length;i++){
		        	 		tableHtml = tableHtml+"<tr bgcolor='#F1F3F5' onmouseover = \"this.style.backgroundColor = '#FFDCB0'\" onmouseout = \"this.style.backgroundColor = ''\" style=''>";
				        	for(var j=0;j<fs.length;j++){
					        	 var strExpress = "records[i]."+fs[j];
				        		 tableHtml = tableHtml+"<td>"+ eval(strExpress) +"</td>";
				        	}
			        	 	tableHtml = tableHtml+"<td><input id=\"recordId"+i+"\" name=\"recordId\" type=\"checkbox\" value=\""+ records[i].id+"\"/>&nbsp;<input type='text' size='5' name='positionSeq' id='positionSeq"+i+"' value='1' /><td></tr>";
			        	}
		        	 }
		        	 var lastRow="<tr><td  colspan=\""+(fls.length+1)+"\"    nowrap background=\"images/bg_table.jpg\" bgcolor=\"#ECEAE6\"  align=\"center\"><input type='hidden' name='recommendTypeId' id='recommendTypeId' value='"+data.recommendTypeId+"'/><input type=\"button\" value=\"提交\" onclick=\"saveRecommend()\"/><td></tr>";
		        	 tableHtml = tableHtml+lastRow+"</table></form>";
		        	// $("#listEditTable").action(tableHtml);
		        	 $("#listEditTable")[0].innerHTML=tableHtml;
	         	}   
	      });
	}
</script>
<style type="text/css">
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
.STYLE1 {
	color: #666666;
	font-weight: bold;
}
.STYLE5 {color: #666666}
.STYLE6 {
	color: #135294;
	font-weight: bold;
}
</style>
<form method="post" action="wizardRecommend.action">
 <table width="95%" border="1" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" align="center">
 	<tr>
 		<td  colspan="4" background="images/bg_table.jpg" bgcolor="#ECEAE6" align="center">推荐向导</td>
 	</tr>
  
	<tr bgcolor="#F1F3F5">
 		<td width="11%">位置</td>
 		<td width="29%">
 			<s:select list="recommendTypes"  listValue="name" listKey="id"  name="recommendTypeId"  
 			id="recommendTypeId"  headerKey="-1" headerValue="--请选择子类型--"  value="productyTypeIdQ"></s:select>
	  </td>
 		<td width="7%">时间</td>	 
	  <td width="53%">
	  	<input class="Wdate" type="text" id="beginDate" name="beginDate" value="<s:property value='beginDate'/>"  onFocus="WdatePicker({isShowClear:false,readOnly:true})"/>
	  		-
	  	<input class="Wdate" type="text" id="endDate" name="endDate" value="<s:property value='endDate'/>" onFocus="WdatePicker({isShowClear:false,readOnly:true})"/>
  	  </td>
	</tr>	
	<tr>
		<td  colspan="4"    nowrap background="images/bg_table.jpg" bgcolor="#ECEAE6"  align="center">
		<input type="button" value="提交" onclick="getRecommend()"/>
	</td>
	</tr>
</table> 
</form>
<br/>
<div id="listRecommendTable"></div>
<br/>

<div id="listEditTable"></div>
